

#include <string>
#include <queue>
#include <sstream>

using namespace std;

/**
 * Definition for a binary tree node.
 */
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

/* 后序遍历优化空间
*/
class Codec {
public:
    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        string s{};
        postOrder(root, s);
        return s;
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        if (data.empty()) return nullptr;
        deque<int> nums{};
        stringstream ss{data};
        string s{};
        while (ss >> s) {
            nums.push_back(stoi(s));
        }
        return helper(INT_MIN, INT_MAX, nums);
    }
private:
    void postOrder(TreeNode* node, string& s) {
        if (node == nullptr) return;
        postOrder(node->left, s);
        postOrder(node->right, s);
        s += to_string(node->val);
        s += " "; // 分隔符
    }
    TreeNode* helper(int lower, int upper, deque<int>& nums) {
        if (nums.empty()) return nullptr;
        int val = nums.back();
        if (val < lower || val > upper) return nullptr;
        nums.pop_back();
        TreeNode* node = new TreeNode(val);
        node->right = helper(val, upper, nums);
        node->left = helper(lower, val, nums);
        return node;
    }
};

#include "../stdc++.h"

class Codec {
public:
    string serialize(TreeNode* root) {
        string res;
        vector<int> arr;
        postOrder(root, arr);
        if (arr.size() == 0) {
            return res;
        }
        for (int i = 0; i < arr.size() - 1; i++) {
            res.append(to_string(arr[i]) + ",");
        }
        res.append(to_string(arr.back()));
        return res;
    }

    TreeNode* deserialize(string data) {
        if (data.size() == 0) {
            return nullptr;
        }
        vector<string> arr = split(data, ',');
        stack<int> st;
        for (auto &str : arr) {
            st.emplace(stoi(str));
        }
        return construct(INT_MIN, INT_MAX, st);
    }

private:
    vector<string> split(const string &str, char dec) {
        int pos = 0;
        int start = 0;
        vector<string> res;
        while (pos < str.size()) {
            while (pos < str.size() && str[pos] == dec) {
                pos++;
            }
            start = pos;
            while (pos < str.size() && str[pos] != dec) {
                pos++;
            }
            if (start < str.size()) {
                res.emplace_back(str.substr(start, pos - start));
            }
        }
        return res;
    }

    void postOrder(TreeNode *root,vector<int>& arr) {
        if (root == nullptr) {
            return;
        }
        postOrder(root->left, arr);
        postOrder(root->right, arr);
        arr.emplace_back(root->val);
    }

    TreeNode * construct(int lower, int upper, stack<int> & st) {
        if (st.size() == 0 || st.top() < lower || st.top() > upper) {
            return nullptr;
        }
        int val = st.top();
        st.pop();
        TreeNode *root = new TreeNode(val);
        root->right = construct(val, upper, st);
        root->left = construct(lower, val, st);
        return root;
    }
};
